package core
{
	import events.Msg_LoadResource;
	
	import flash.display.Stage;
	import flash.events.Event;
	
	import interfaces.IAction;
	import interfaces.IHookListener;
	import interfaces.IMsgListener;
	import interfaces.IVersion;
	
	import module.ResourceManager;
	import module.VersionManager;

	public class Global
	{
		public static var MODULE:String = "Version";
		public static var pause:Boolean;
		private static var m_global:Global;
		private var m_msgManager:MsgManager;		// 消息管理中心
		private var m_dlCenter:DownloadCenter;		// 下载中心
		private var m_version:VersionManager;		// 版本控制
		private var m_resource:ResourceManager;	// 显示资源管理
		private var m_action:ActionManager;		// 动画管理器
		private static var m_stage:Stage;
		
		public function Global(s:Simpleton)
		{
			
		}
		
		public static function init(s:Stage):Global
		{
			if(!m_global)
			{
				m_global = new Global(new Simpleton());
				m_global.m_msgManager = new MsgManager();
				m_global.m_dlCenter = new DownloadCenter();
				m_global.m_version = new VersionManager();
				m_global.m_action = new ActionManager();
				m_global.m_resource = new ResourceManager();
				Global.stage = s;
			}
			
			return m_global;
		}
		
		/*
		必要的功能模块 set/get
		*/
		
		public static function get stage():Stage
		{
			return m_stage;
		}
		
		public static function set stage(s:Stage):void
		{
			m_stage = s;
		}
		
		public function get msgManager():MsgManager
		{
			return m_msgManager;
		}
		
		public function set msgManager(mm:MsgManager):void
		{
			m_msgManager = mm;
		}
		
		public function get dlCenter():DownloadCenter
		{
			return m_dlCenter;
		}
		
		public function set dlCenter(dl:DownloadCenter):void
		{
			m_dlCenter = dl;
		}
		
		public function get ver():VersionManager
		{
			return m_version;
		}
		
		public function set ver(v:VersionManager):void
		{
			m_version = v;
		}
		
		public function get resManager():ResourceManager
		{
			return m_resource;
		}
		
		public function set resManager(res:ResourceManager):void
		{
			m_resource = res;
		}
		// ---------------------------消息中心---------------------------\\
		
		public static function addListener(msg:int,msgListener:IMsgListener):void
		{
			m_global.msgManager.addListener(Global.MODULE + msg,msgListener);
		}
		
		public static function removeListener(msg:int,msgListener:IMsgListener):void
		{
			m_global.msgManager.addListener(Global.MODULE+ msg,msgListener);
		}
		
		public static function addListeners(msgs:Array,msgListener:IMsgListener):void
		{
			for(var i:int = 0; i < msgs.length; i++)
			{
				m_global.msgManager.addListener(Global.MODULE + msgs[i],msgListener);
			}
		}
		
		public static function removeListeners(msgs:Array,msgListener:IMsgListener):void
		{
			for(var i:int = 0; i < msgs.length; i++)
			{
				m_global.msgManager.removeListener(Global.MODULE + msgs[i],msgListener);
			}
		}
		
		public static function dispatchEvent(msg:int,data:Object = null):void
		{
			m_global.msgManager.dispatchEvent(new DataEvent(Global.MODULE +  msg,data));
		}
		
		public static function hookMsg(ihl:IHookListener):void
		{
			m_global.msgManager.hookMsg(ihl);
		}
		
		public static function unhookMsg(ihl:IHookListener):void
		{
			m_global.msgManager.unhookMsg(ihl);
		}
		
		// ---------------------------/消息中心---------------------------\\
		// ---------------------------加载中心------------------------------\\
		public static function loadSwf(url:String):void
		{
			m_global.dlCenter.loadSwf(url);
		}
		
		public static function loadSwfs(urls:Vector.<String>):void
		{
			m_global.dlCenter.loadSwfs(urls);
		}
		
		public static function loadResource(url:String,cls:String,target:* = null):*
		{
			var msg:Msg_LoadResource = Msg_LoadResource.createMessage(url,cls,target);
			Global.dispatchEvent(Msg_LoadResource.M_LOADRESOURCE,msg);
			return msg.atom;
		}
		// ---------------------------/加载中心------------------------------\\
		// --------------------------版本信息-----------------------------\\
		public static function getVersion(url:String):Number
		{
			return m_global.m_version.getVersion(url);
		}
		// --------------------------/版本信息-----------------------------\\
		// -------------------------动画管理-------------------------------\\
		public static function addAction(a:IAction):void
		{
			m_global.m_action.addAction(a);
		}
		
		public static function deleteAction(a:IAction):void
		{
			m_global.m_action.deleteAction(a);
		}
		
		public static function updateAction():void
		{
			m_global.m_action.update();
		}
		
		public static function clearAllAction():void
		{
			m_global.m_action.clearAllAction();
		}
		
		public static function pauseAllAction(b:Boolean):void
		{
			pause = b;
			m_global.m_action.pauseAllAction(b);
		}
		// -------------------------/动画管理-------------------------------\\
	}
}

class Simpleton{}